package demo.practice.leetcode_eveyday;

import demo.practice.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class P783 {

    int min=Integer.MAX_VALUE;

    public int minDiffInBST(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        foreach(root,list);



//        for(int i=0;i<list.size()-1;i++){
//            if(Math.abs(list.get(i)-list.get(i+1))<min )
//                min=Math.abs(list.get(i)-list.get(i+1));
//        }

        return min;
    }

    private void foreach(TreeNode root, List<Integer> list) {

        if (root == null)
            return;

        if (root.left != null){
            foreach(root.left, list);

        }

        list.add(root.val);
        if(list.size()>1 ){
            //list.get(list.size()-1)
            //list.get(list.size()-1)
            min= Math.min(Math.abs(list.get(list.size()-1)-list.get(list.size()-2) ) ,min);
        }

        if (root.right != null)
            foreach(root.right, list);
    }

}

